home *** CD-ROM | disk | FTP | other *** search
- unit MLBMain;
-
- interface
-
- uses
- Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
- Grids, ComCtrls, ExtCtrls, StdCtrls, Menus, adGrids, MLBData;
-
- type
-
- TFormMain = class(TForm)
- adDrawGrid1: TadDrawGrid;
- Ascending1: TMenuItem;
- CheckBoxColor: TCheckBox;
- CheckBoxEllipsis: TCheckBox;
- CheckBoxExpand: TCheckBox;
- CheckBoxFont: TCheckBox;
- CheckBoxHighlight: TCheckBox;
- ComboBoxAlignment: TComboBox;
- Descending1: TMenuItem;
- ImageList1: TImageList;
- LabelAlignment: TLabel;
- PanelProperty: TPanel;
- PanelSplitter: TPanel;
- PopupMenu1: TPopupMenu;
- RichEdit: TRichEdit;
- TabDemo: TTabControl;
- TabDivisions: TTabControl;
- procedure adDrawGrid1DrawBitmap(Sender: TObject; ACol, ARow: Longint;
- State: TGridDrawState; var ImageIndex: Integer;
- var Alignment: TadCellAlignment; var Margin: Integer);
- procedure adDrawGrid1DrawText(Sender: TObject; ACol, ARow: Longint;
- State: TGridDrawState; var Font: TFont; var Color: TColor;
- var Align: TadCellAlignment; var Margin: Integer);
- procedure adDrawGrid1GetText(Sender: TObject; ACol, ARow: Longint;
- var Value: string);
- procedure adDrawGrid1TitleClick(Sender: TObject; Button: TMouseButton;
- Shift: TShiftState; ACol: Longint);
- procedure Ascending1Click(Sender: TObject);
- procedure Descending1Click(Sender: TObject);
- procedure FormCreate(Sender: TObject);
- procedure CheckBoxColorClick(Sender: TObject);
- procedure CheckBoxColorEnter(Sender: TObject);
- procedure CheckBoxEllipsisClick(Sender: TObject);
- procedure CheckBoxEllipsisEnter(Sender: TObject);
- procedure CheckBoxFontClick(Sender: TObject);
- procedure CheckBoxFontEnter(Sender: TObject);
- procedure CheckBoxHighlightClick(Sender: TObject);
- procedure CheckBoxHighlightEnter(Sender: TObject);
- procedure ComboBoxAlignmentChange(Sender: TObject);
- procedure ComboBoxAlignmentEnter(Sender: TObject);
- procedure CheckBoxExpandEnter(Sender: TObject);
- procedure CheckBoxExpandClick(Sender: TObject);
- procedure TabDemoChange(Sender: TObject);
- procedure TabDivisionsChange(Sender: TObject);
- private
- { Private declarations }
- public
- procedure InitPopup(Ascending: Boolean);
- procedure ResortColumn(ACol: Integer);
- procedure ShowDemoText(const Name: String);
- end;
-
- var
- FormMain: TFormMain;
- SortCol: Integer;
- CurrentDivision: TMLBDivision;
-
- implementation
-
- {$R *.DFM}
-
- {$R STRINGS.RES}
-
- {
- Component User's Note: The following adDrawGrid event-handler methods
- demonstrate the way AutoDraw grids allow run-time specification of grid
- attributes. See the appropriate event-handler topics in the help file for
- more details on how to use these events.
- }
-
- {
- adDrawGrid1DrawBitmap is the OnDrawBitmap event-handler for the AutoDraw grid.
- }
- procedure TFormMain.adDrawGrid1DrawBitmap(Sender: TObject; ACol,
- ARow: Longint; State: TGridDrawState; var ImageIndex: Integer;
- var Alignment: TadCellAlignment; var Margin: Integer);
- begin
-
- { Exit if we are on a title row or not in first column }
- if (ARow = -1) or (ACol <> 0) then Exit;
-
- { Draw bitmap in first column. BitmapIndex is hardcoded in MLBData }
- Margin := 0;
- ImageIndex := CurrentDivision.Items[ARow].BitmapIndex;
-
- end;
-
- {
- adDrawGrid1DrawText is the OnDrawText event-handler for the AutoDraw grid.
- }
- procedure TFormMain.adDrawGrid1DrawText(Sender: TObject; ACol, ARow: Longint;
- State: TGridDrawState; var Font: TFont; var Color: TColor;
- var Align: TadCellAlignment; var Margin: Integer);
- begin
-
- { Exit if we are on a title row or not in fifth column }
- if (aRow = -1) or (ACol <> 4) then Exit;
-
- { Display percents < .500 in red }
- if (CurrentDivision[ARow].Percent < 0.5) then Font.Color := clRed;
- end;
-
- {
- adDrawGrid1GetText is the OnDrawText for event-handler the AutoDraw grid.
- }
- procedure TFormMain.adDrawGrid1GetText(Sender: TObject; ACol, ARow: Longint;
- var Value: string);
-
- { Formats Games Back for grid display }
- function FormatGB(GB: Extended): String;
- begin
- Result := Format('%.1f',[GB]);
- if (Result = '0.0') then Result := '-';
- if Result[3] = '0' then Result := Result[1];
- end;
-
- { Formats Streak for grid display }
- function FormatStreak(Streak: Integer): String;
- begin
- if Streak < 0 then Result := 'Lost' else Result := 'Won';
- Result := Result+' '+IntToStr(Abs(Streak));
- end;
-
- begin
-
- { Exit if we are on a title row }
- if (ARow = -1) then Exit;
-
- { Otherwise, provide formatted text for normal rows }
- with CurrentDivision[ARow] do
- begin
- { Set Value to fully formatted string, depending on column }
- case ACol of
- 1 : Value := Name;
- 2 : Value := IntToStr(Won);
- 3 : Value := IntToStr(Lost);
- 4 : Value := Format('%.3f',[Percent]);
- 5 : Value := FormatGB(GamesBack);
- 6 : Value := Format('%.2d',[HomeWon])+'-'+Format('%.2d',[HomeLost]);
- 7 : Value := Format('%.2d',[RoadWon])+'-'+Format('%.2d',[RoadLost]);
- 8 : Value := FormatStreak(Streak);
- end;
- end;
-
- end;
-
- {
- adDrawGrid1TitleClick is the OnTitleClick event-handler for the AutoDraw grid.
- }
- procedure TFormMain.adDrawGrid1TitleClick(Sender: TObject;
- Button: TMouseButton; Shift: TShiftState; ACol: Longint);
- var
- NewOrder : TMLBSortOrder;
- begin
-
- if (Button <> mbLeft) then Exit;
-
- { Resort on newly clicked column }
- if (ACol <> SortCol) then
- begin
- if (ACol = 1) or (ACol = 5) then InitPopup(True) else InitPopup(False);
- adDrawGrid1.Columns[SortCol].Title.Font.Style := [];
- end
- else
- begin
- InitPopup(not Ascending1.Checked);
- end;
- ResortColumn(ACol);
-
- end;
-
- {
- Programmer's Note: The following methods are used by the demonstration to
- implement the new (Delphi 2.0) TList.Sort method, and the attendant
- user-interface elements. For more details, see the MLBData unit, and the
- Delphi 2.0 help.
- }
-
- {
- Ascending1Click is the event-handler for the OnClick event of the Ascending
- popup menu item.
- }
- procedure TFormMain.Ascending1Click(Sender: TObject);
- begin
- { Resort, given new ascending/descending value }
- InitPopup(True);
- ResortColumn(SortCol);
- end;
-
- {
- Descending1Click is the event-handler for the OnClick event of the Descending
- popup menu item.
- }
- procedure TFormMain.Descending1Click(Sender: TObject);
- begin
- InitPopup(False);
- ResortColumn(SortCol);
- end;
-
- {
- ResortColumn is an internal method which sort the MLB data using the new Delphi
- TList.Sort method.
- }
- procedure TFormMain.ResortColumn(ACol: Integer);
- var
- NewOrder : TMLBSortOrder;
- begin
-
- { Determine new sort order }
- case ACol of
- 1: NewOrder := soTeam;
- 2: NewOrder := soWon;
- 3: NewOrder := soLost;
- 4: NewOrder := soPercent;
- 5: NewOrder := soGamesBack;
- end;
-
- { Resort according to new sort order }
- SortCol := ACol;
- CurrentDivision.Sort(NewOrder,Ascending1.Checked);
-
- { Display sorted column title in bold }
- adDrawGrid1.Columns[ACol].Title.Font.Style := [fsBold];
-
- { Redisplay grid }
- adDrawGrid1.Row := 1;
- adDrawGrid1.Invalidate;
-
- end;
-
- {
- InitPopup toggles the ascending/descending popup menu check.
- }
- procedure TFormMain.InitPopup(Ascending: Boolean);
- begin
- Ascending1.Checked := Ascending;
- Descending1.Checked := not Ascending;
- end;
-
- {
- Programmer's Note: the following methods implement the demo's user-interface
- elements, including the division selector tab.
- }
-
- {
- FormCreate is the OnCreate event-handler for the form. FormCreate initializes
- the user-interface elements.
- }
- procedure TFormMain.FormCreate(Sender: TObject);
- begin
- { Initial sorted column is Games Back ('GB') }
- SortCol := 5;
- { Initialize user-interface elements }
- InitPopup(True);
- TabDivisions.TabIndex := 0;
- TabDemo.TabIndex := 0;
- TabDivisionsChange(Self);
- TabDemoChange(Self);
- ComboBoxAlignment.ItemIndex := 3;
- end;
-
- {
- TabDivisionsChange is the OnChange event-handler for the baseball divisions tab.
- TabDivisionsChange synchronizes the CurrentDivision global variable with the
- currently selected tab.
- }
- procedure TFormMain.TabDivisionsChange(Sender: TObject);
- var
- NewOrder : TMLBSortOrder;
- begin
-
- { Synchronize tab and division }
- case TabDivisions.TabIndex of
- 0 : CurrentDivision := ALEast;
- 1 : CurrentDivision := ALCentral;
- 2 : CurrentDivision := ALWest;
- end;
-
- { Set rows to reflect new division }
- adDrawGrid1.RowCount := CurrentDivision.Count+1;
-
- { Resort }
- ResortColumn(SortCol);
-
- end;
-
- {
- Component User's Note: The following methods support the user-interface
- elements of the 'Column Properties' panel, which demonstrate run-time
- modification of the AutoDraw grid's properties. Each user-interface element
- has an OnEnter event which displays explanatory text (loaded from the
- application resource table) and an OnChange event which modifies the grid's
- properties. See the appropriate property topics in the help file for more
- details on how to use these events.
- }
-
- procedure TFormMain.CheckBoxColorClick(Sender: TObject);
- begin
- if CheckBoxColor.Checked then
- begin
- adDrawGrid1.Columns[5].Color := clWindow;
- adDrawGrid1.Columns[5].Font.Color := clBlack
- end
- else
- begin
- adDrawGrid1.Columns[5].Color := clTeal;
- adDrawGrid1.Columns[5].Font.Color := clWhite;
- end;
- end;
-
- procedure TFormMain.CheckBoxColorEnter(Sender: TObject);
- begin
- ShowDemoText('demo1b');
- end;
-
- procedure TFormMain.CheckBoxEllipsisClick(Sender: TObject);
- begin
- adDrawGrid1.Columns[1].Ellipsis := CheckBoxEllipsis.Checked;
- end;
-
- procedure TFormMain.CheckBoxEllipsisEnter(Sender: TObject);
- begin
- ShowDemoText('demo1d');
- end;
-
- procedure TFormMain.CheckBoxExpandClick(Sender: TObject);
- begin
- adDrawGrid1.Columns[1].AutoExpand := CheckBoxExpand.Checked;
- end;
-
- procedure TFormMain.CheckBoxExpandEnter(Sender: TObject);
- begin
- ShowDemoText('demo1c');
- end;
-
- procedure TFormMain.CheckBoxFontClick(Sender: TObject);
- begin
- if CheckBoxFont.Checked then
- adDrawGrid1.Font.Color := clBlue else
- adDrawGrid1.Font.Color := clBlack;
- end;
-
- procedure TFormMain.CheckBoxFontEnter(Sender: TObject);
- begin
- ShowDemoText('demo1a');
- end;
-
- procedure TFormMain.CheckBoxHighlightClick(Sender: TObject);
- begin
- if CheckBoxHighlight.Checked then
- begin
- adDrawGrid1.Highlight := clGray;
- adDrawGrid1.HighlightText := clWhite;
- end
- else
- begin
- adDrawGrid1.Highlight := clHighlight;
- adDrawGrid1.HighlightText := clHighlightText;
- end;
- end;
-
- procedure TFormMain.CheckBoxHighlightEnter(Sender: TObject);
- begin
- ShowDemoText('demo1e');
- end;
-
- procedure TFormMain.ComboBoxAlignmentChange(Sender: TObject);
- begin
- adDrawGrid1.Columns[1].Alignment :=
- TadCellAlignment(ComboBoxAlignment.ItemIndex);
- end;
-
- procedure TFormMain.ComboBoxAlignmentEnter(Sender: TObject);
- begin
- ShowDemoText('demo1f');
- end;
-
- procedure TFormMain.TabDemoChange(Sender: TObject);
- begin
- PanelProperty.Visible := (TabDemo.TabIndex = 0);
- case TabDemo.TabIndex of
- 0 : ShowDemoText('demo1');
- 1 : ShowDemoText('demo2');
- 2 : ShowDemoText('demo3');
- 3 : ShowDemoText('demo4');
- end;
- end;
-
- procedure TFormMain.ShowDemoText(const Name: String);
- var
- Stream : TResourceStream;
- begin
- Stream := TResourceStream.Create(hInstance,Uppercase(Name),'RTFDATA');
- try
- RichEdit.Lines.LoadFromStream(Stream);
- finally
- Stream.Free;
- end;
- end;
-
- end.
-